package com.capinfo.controller;

import com.alibaba.fastjson.JSONObject;
import com.capinfo.base.BaseController;
import com.capinfo.base.CurrentUser;
import com.capinfo.core.annotation.Log;
import com.capinfo.entity.CapServiceKind;
import com.capinfo.service.CapServiceKindService;
import com.capinfo.util.JsonUtil;
import com.capinfo.util.ReType;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author: sunbenjin
 * @date: 2020/2/29
 **/
@Controller
@RequestMapping(value="/kind")
public class CapServiceKindController extends BaseController {

    @Autowired
    private CapServiceKindService capServiceKindService;

    @GetMapping(value = "show")
    @RequiresPermissions("kind:show")
    public String show(Model model) {
        return "/servicekind/service-kind-list";
    }
    @ApiOperation(value = "/showKindList", httpMethod = "GET", notes = "展示机构列表")
    @GetMapping(value = "showKindList")
    @ResponseBody
    public ReType showKind(Model model, CapServiceKind capServiceKind, String page, String limit) {
        if(StringUtils.isEmpty(page)){
            page = "0";
        }
        if(StringUtils.isEmpty(limit)){
            limit = "10";
        }
        return capServiceKindService.show(capServiceKind, Integer.valueOf(page), Integer.valueOf(limit));
    }
    @GetMapping(value = "addKind")
    @RequiresPermissions("kind:show")
    public String addKind(Model model, HttpServletRequest request, CapServiceKind capServiceKind) {
        String pId = request.getParameter("pId");
        model.addAttribute("capServiceKind",capServiceKind);
        return "/servicekind/service-kind-form";
    }
    @GetMapping(value = "showKind")
    @RequiresPermissions("kind:show")
    public String showKind(Model model, HttpServletRequest request) {
       String id = request.getParameter("id");
       CapServiceKind capServiceKind = capServiceKindService.selectByPrimaryKey(id);

       String detail = request.getParameter("detail");
       model.addAttribute("detail",detail);
        model.addAttribute("capServiceKind",capServiceKind);
        return "/servicekind/service-kind-form";
    }

    @ApiOperation(value="addKind",httpMethod = "POST",notes="添加或更新业务类型名称")
    @PostMapping("addKind")
    @ResponseBody
    public JsonUtil addType(CapServiceKind capServiceKind){
        JsonUtil j = new JsonUtil();
        CurrentUser user = (CurrentUser) SecurityUtils.getSubject().getSession().getAttribute("curentUser");
        try {
            if(StringUtils.isEmpty(capServiceKind.getId())){
                capServiceKind.setId(UUID.randomUUID().toString().replaceAll("-", ""));
                capServiceKind.setCreateBy(user.getId());
                capServiceKind.setCreateDate(new Date());
                capServiceKindService.insertSelective(capServiceKind);
            }else{
                capServiceKind.setUpdateBy(user.getId());
                capServiceKind.setUpdateDate(new Date());
                capServiceKindService.updateByPrimaryKeySelective(capServiceKind);
            }

            j.setMsg("保存成功");
        } catch (Exception e) {
            j.setMsg("保存失败");
            j.setFlag(false);

            e.printStackTrace();
        }
        return j;
    }

    @ApiOperation(value="get",httpMethod = "GET",notes="根据id得业务分类实例")
    @GetMapping("get")
    @ResponseBody
    public JsonUtil get(String id){
        JsonUtil j = new JsonUtil();
        try {
            CapServiceKind capServiceKind = capServiceKindService.selectByPrimaryKey(id);
            JSONObject jsonObject = new JSONObject();
            if(capServiceKind==null){

                capServiceKind = new CapServiceKind();
                capServiceKind.setName("");
            }
            jsonObject.put("data",capServiceKind);
            j.setJsonObj(jsonObject);
            j.setMsg("加载业务分类成功");
        } catch (Exception e) {
            j.setMsg("加载业务分类失败");
            j.setFlag(false);

            e.printStackTrace();
        }
        return j;
    }
    @ApiOperation(value="getKindList",httpMethod = "GET",notes="根据id得业务类型得到业务分类")
    @GetMapping("getKindList")
    @ResponseBody
    public JsonUtil getKindList(String serviceTypeId){
        JsonUtil j = new JsonUtil();
        try {
            if(StringUtils.isNotEmpty(serviceTypeId)){
                CapServiceKind capServiceKind = new CapServiceKind();
                capServiceKind.setServiceTypeId(serviceTypeId);
                List<CapServiceKind> list = capServiceKindService.selectListByPage(capServiceKind);
                if(!CollectionUtils.isEmpty(list)){
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("data",list);
                    j.setJsonObj(jsonObject);
                    j.setMsg("获取成功");
                }else{
                    j.setFlag(false);
                    j.setMsg("获取失败");
                }
            }else{
                j.setFlag(false);
                j.setMsg("获取失败");
            }
        } catch (Exception e) {
            j.setMsg("获取失败");
            j.setFlag(false);
            e.printStackTrace();
        }
        return j;
    }

    @Log(desc = "删除", type = Log.LOG_TYPE.DEL)
    @ApiOperation(value = "/del", httpMethod = "POST", notes = "删除")
    @PostMapping(value = "/del")
    @ResponseBody
    @RequiresPermissions("dict:del")
    public JsonUtil del(String id) {
//        CapServiceKind capServiceKind = capServiceKindService.selectByPrimaryKey(id);
        //逻辑删除
        return capServiceKindService.delById(id,true);
    }



}
